package io.hilo.example.demo.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.jretty.util.Const;
import org.jretty.util.NestedRuntimeException;
import org.jretty.util.zip.ZipException;
import org.jretty.util.zip.ZipUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import org.hilo.boot.core.UT;


/**
 * Service Demo
 * 业务逻辑处理
 * 
 * @author xxxx
 * @since 2023-10-12
 */
@Service
public class GeneratorService2 {
    private static Logger LOGGER = LoggerFactory.getLogger(GeneratorService2.class);
    
    public byte[] generatorCode(String useType, String groupId, String artifactId, String basePackage) {
        return test(useType, groupId, artifactId, basePackage);
    }
    
    public static void main0(String[] args) throws IOException {
        byte[] aa = test("admin", "com.company", "pay-admin", "com.company.project");
        if (aa != null) {
            System.out.println("00000000000=" + aa.length);
        }
    }
    
    public static byte[] test(String useType, String groupId, String artifactId, String basePackage) {
        if (basePackage == null) {
            basePackage = groupId;
        }
        if (useType.equals("admin")) {
            useType = "hilo-demo-admin";
            try {
                return copyAdmin(artifactId);
            } catch (IOException e) {
                return null;
            }
        } else if (useType.equals("web")) {
            useType = "hilo-demo-web";
            try {
                return copyweb2(artifactId);
            } catch (IOException e) {
                return null;
            }
        } else if (useType.equals("dubbo")) {
            useType = "hilo-demo-dubbo";
            try {
                return copyDubbo(artifactId);
            } catch (IOException e) {
                return null;
            }
        } else {
            return null;
        }

    }
    
//    public static File createTempDir(String name) throws IOException {
//        File tempDir = File.createTempFile(name, null);
//        tempDir.delete();
//        tempDir.mkdir();
//        tempDir.deleteOnExit();
//        return tempDir;
//    }
    
    public static File createTempDir(String folderName) {
        File tmpdir = new File(System.getProperty("java.io.tmpdir"));
        tmpdir = new File(tmpdir, folderName);
        if (!tmpdir.exists()) {
            tmpdir.mkdir();
        }
        return tmpdir;
    }
    
    static final String BASE_PATH = "D:\\__SYNC2\\git\\hilo\\hilo-demo\\hilo-demo-all\\hilodemo-all";
    
    static byte[] copyweb2(String artifactId) throws IOException {
        String rpath = UT.Random.getRadomStr09az(10);
        File path = createTempDir(rpath);
        path.deleteOnExit();
        System.out.println("------------------path=" + path);
        
        String target = path.getAbsolutePath() + "\\" + artifactId + "-parent\\";
        String targetZip = path.getAbsolutePath() + "\\" + artifactId + "-parent.zip";
        UT.File.deleteAll(new File(target));
        new File(target).mkdir();
        
        String content = UT.File.getTextContent(new FileInputStream("D:\\__SYNC2\\git\\hilo\\hilo-demo\\hilo-demo-all\\pom-simple.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+"\\pom.xml", content);
        
        new File(target+artifactId+"-common").mkdirs();
        UT.File.copyFolder(BASE_PATH+"-common", target+artifactId+"-common", false);
        UT.File.deleteAll(new File(target+artifactId+"-common\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\.project"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"-common\\pom.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"-common\\pom.xml", content);
        
        new File(target+artifactId+"-biz").mkdir();
        UT.File.copyFolder(BASE_PATH+"-biz", target+artifactId+"-biz", false);
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.project"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\src\\main\\java\\io\\hilo\\example\\demobar\\api"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-web-db.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-dubbo.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-admin.xml"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"-biz\\pom-web.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"-biz\\pom.xml", content);
        new File(target+artifactId+"-biz\\pom-web.xml").delete();
        
        new File(target+artifactId).mkdir();
        UT.File.copyFolder(BASE_PATH, target+artifactId, false);
        UT.File.deleteAll(new File(target+artifactId+"\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"\\target"));
        UT.File.deleteAll(new File(target+artifactId+"\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"\\.project"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationDB.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationDubbo.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\ShiroFilterConfig.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\RedisConfigFoo2.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\DynamicDataSourceAopConfig.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\DefaultPointcuts.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\DataSourceKey.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\AppExceptionResolver.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\WebMvcConfig.java"));
//        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app.properties"));
//        new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app-pub.properties")
//        .renameTo(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app.properties"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app-pub.properties"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application-admin-main.yml"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application-dubbo.yml"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application-dubbo-main.yml"));
        
        UT.File.deleteAll(new File(target+artifactId+"\\pom.xml"));
        UT.File.deleteAll(new File(target+artifactId+"\\pom-web-tomcat.xml"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"\\pom-web.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"\\pom.xml", content);
        new File(target+artifactId+"\\pom-web.xml").delete();
        
        UT.File.deleteAll(new File(target+artifactId+"\\src\\sql"));
        
        
        
        try {
            ZipUtils.zipFile(targetZip, target);
        } catch (ZipException e) {
            e.printStackTrace();
        }
        
        UT.File.deleteAll(new File(target));
        
        System.out.println("----------------------------------------done!");
        byte[] bret=null;
        try {
            bret = FileUtils.readFileToByteArray(new File(targetZip));
        } catch (IOException e) {
            e.printStackTrace();
        }
        new File(targetZip).delete();
        UT.File.deleteAll(path);
        return bret;
        
    }
    
    
    static byte[] copyAdmin(String artifactId) throws IOException {
        String rpath = UT.Random.getRadomStr09az(10);
        File path = createTempDir(rpath);
        path.deleteOnExit();
        System.out.println("------------------path=" + path);
        
        String target = path.getAbsolutePath() + "\\" + artifactId + "-parent\\";
        String targetZip = path.getAbsolutePath() + "\\" + artifactId + "-parent.zip";
        UT.File.deleteAll(new File(target));
        new File(target).mkdir();
        
        String content = UT.File.getTextContent(new FileInputStream("D:\\__SYNC2\\git\\hilo\\hilo-demo\\hilo-demo-all\\pom-simple.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+"\\pom.xml", content);
        
        new File(target+artifactId+"-common").mkdirs();
        UT.File.copyFolder(BASE_PATH+"-common", target+artifactId+"-common", false);
        UT.File.deleteAll(new File(target+artifactId+"-common\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\.project"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"-common\\pom.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"-common\\pom.xml", content);
        
        new File(target+artifactId+"-biz").mkdir();
        UT.File.copyFolder(BASE_PATH+"-biz-db", target+artifactId+"-biz", false);
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\.project"));
        UT.File.copyFolder(BASE_PATH+"-biz", target+artifactId+"-biz", false);
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.project"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\src\\main\\java\\io\\hilo\\example\\demobar\\api"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-web.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-web-db.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-dubbo.xml"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"-biz\\pom-admin.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"-biz\\pom.xml", content);
        new File(target+artifactId+"-biz\\pom-admin.xml").delete();
        
        new File(target+artifactId).mkdir();
        UT.File.copyFolder(BASE_PATH, target+artifactId, false);
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationAdmin.java"));
        content = UT.Str.replace(content, "StartApplicationAdmin", "StartApplication");
        replaceFile(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplication.java", content);
        new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationAdmin.java").delete();
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationDubbo.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\RedisConfigFoo2.java"));
//        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app.properties"));
//        new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app-pub.properties")
//        .renameTo(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app.properties"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app-pub.properties"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application.yml"));
        new File(target+artifactId+"\\src\\main\\resources\\application-admin-main.yml")
        .renameTo(new File(target+artifactId+"\\src\\main\\resources\\application.yml"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application-dubbo.yml"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application-dubbo-main.yml"));
        
        UT.File.deleteAll(new File(target+artifactId+"\\pom.xml"));
        UT.File.deleteAll(new File(target+artifactId+"\\pom-web-tomcat.xml"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"\\pom-web.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"\\pom.xml", content);
        new File(target+artifactId+"\\pom-web.xml").delete();
        
        //UT.File.deleteAll(new File(target+artifactId+"\\src\\sql"));
        
        
        
        try {
            ZipUtils.zipFile(targetZip, target);
        } catch (ZipException e) {
            e.printStackTrace();
        }
        
        UT.File.deleteAll(new File(target));
        
        System.out.println("----------------------------------------done!");
        byte[] bret=null;
        try {
            bret = FileUtils.readFileToByteArray(new File(targetZip));
        } catch (IOException e) {
            e.printStackTrace();
        }
        new File(targetZip).delete();
        UT.File.deleteAll(path);
        return bret;
        
    }
    
    static byte[] copyDubbo(String artifactId) throws IOException {
        String rpath = UT.Random.getRadomStr09az(10);
        File path = createTempDir(rpath);
        path.deleteOnExit();
        System.out.println("------------------path=" + path);
        
        String target = path.getAbsolutePath() + "\\" + artifactId + "-parent\\";
        String targetZip = path.getAbsolutePath() + "\\" + artifactId + "-parent.zip";
        UT.File.deleteAll(new File(target));
        new File(target).mkdir();
        
        String content = UT.File.getTextContent(new FileInputStream("D:\\__SYNC2\\git\\hilo\\hilo-demo\\hilo-demo-all\\pom.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+"\\pom.xml", content);
        
        new File(target+artifactId+"-api").mkdirs();
        UT.File.copyFolder(BASE_PATH+"-api", target+artifactId+"-api", false);
        UT.File.deleteAll(new File(target+artifactId+"-api\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-api\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-api\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-api\\.project"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"-api\\pom.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"-api\\pom.xml", content);
        
        new File(target+artifactId+"-common").mkdirs();
        UT.File.copyFolder(BASE_PATH+"-common", target+artifactId+"-common", false);
        UT.File.deleteAll(new File(target+artifactId+"-common\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-common\\.project"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"-common\\pom.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"-common\\pom.xml", content);
        
        new File(target+artifactId+"-biz").mkdir();
        UT.File.copyFolder(BASE_PATH+"-biz-db", target+artifactId+"-biz", false);
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-biz-db\\.project"));
        UT.File.copyFolder(BASE_PATH+"-biz", target+artifactId+"-biz", false);
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\target"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\.project"));
//        UT.File.deleteAll(new File(target+artifactId+"-biz\\src\\main\\java\\io\\hilo\\example\\demobar\\api"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-admin.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-web.xml"));
        UT.File.deleteAll(new File(target+artifactId+"-biz\\pom-web-db.xml"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"-biz\\pom-dubbo.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"-biz\\pom.xml", content);
        new File(target+artifactId+"-biz\\pom-dubbo.xml").delete();
        
        new File(target+artifactId).mkdir();
        
        UT.File.copyFolder(BASE_PATH, target+artifactId, false);
        UT.File.deleteAll(new File(target+artifactId+"\\.settings"));
        UT.File.deleteAll(new File(target+artifactId+"\\target"));
        UT.File.deleteAll(new File(target+artifactId+"\\.classpath"));
        UT.File.deleteAll(new File(target+artifactId+"\\.project"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationDubbo.java"));
        content = UT.Str.replace(content, "StartApplicationDubbo", "StartApplication");
        replaceFile(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplication.java", content);
        new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationDubbo.java").delete();
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationAdmin.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\StartApplicationDB.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\ShiroFilterConfig.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\RedisConfigFoo2.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\AppExceptionResolver.java"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\java\\io\\hilo\\example\\config\\WebMvcConfig.java"));
//        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app.properties"));
//        new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app-pub.properties")
//        .renameTo(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app.properties"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\META-INF\\app-pub.properties"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application.yml"));
        new File(target+artifactId+"\\src\\main\\resources\\application-dubbo-main.yml")
        .renameTo(new File(target+artifactId+"\\src\\main\\resources\\application.yml"));
        UT.File.deleteAll(new File(target+artifactId+"\\src\\main\\resources\\application-admin-main.yml"));
        
        UT.File.deleteAll(new File(target+artifactId+"\\pom.xml"));
        UT.File.deleteAll(new File(target+artifactId+"\\pom-web-tomcat.xml"));
        content = UT.File.getTextContent(new FileInputStream(target+artifactId+"\\pom-web.xml"));
        content = UT.Str.replace(content, "hilodemo-all", artifactId);
        replaceFile(target+artifactId+"\\pom.xml", content);
        new File(target+artifactId+"\\pom-web.xml").delete();
        
        //UT.File.deleteAll(new File(target+artifactId+"\\src\\sql"));
        
        try {
            ZipUtils.zipFile(targetZip, target);
        } catch (ZipException e) {
            e.printStackTrace();
        }
        
        UT.File.deleteAll(new File(target));
        
        System.out.println("----------------------------------------done!");
        byte[] bret=null;
        try {
            bret = FileUtils.readFileToByteArray(new File(targetZip));
        } catch (IOException e) {
            e.printStackTrace();
        }
        new File(targetZip).delete();
        UT.File.deleteAll(path);
        return bret;
        
    }
    
    
    public static void replaceFile(String fullFileName, String content) {
        BufferedWriter bw = null;
        try {

            bw = UT.File.getBufferedWriter(fullFileName, false, Const.UTF_8);
            bw.write(content);
            bw.flush();

        } catch (IOException e) {
            throw new NestedRuntimeException(e);
        } finally {
            UT.IO.closeIO(bw);
        }
    }
        
    
    

}
